package com.tanhua.autoconfig.template;

import com.tanhua.autoconfig.properties.SmsProperties;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

public class SmsTemplate {

    private SmsProperties properties;

    private RestTemplate restTemplate;

    public SmsTemplate(SmsProperties properties, RestTemplate restTemplate) {
        this.properties = properties;
        this.restTemplate = restTemplate;
    }

    private static String Url = "http://106.ihuyi.com/webservice/sms.php?method=Submit";

    public boolean sendSms(String mobile, String validateCode) {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        //提交参数设置
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("account", properties.getAccessKey());
        map.add("password", properties.getSecret());
        map.add("mobile", mobile);
        map.add("content", "您的验证码是：" + validateCode + "。请不要把验证码泄露给其他人。");
        map.add("format", "json");

        // 组装请求体
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);

        // 发送post请求，并打印结果，以String类型接收响应结果JSON字符串
        Map<String, Object> result = null;
        try {
            result = restTemplate.postForObject(Url, request, Map.class);
            if (result.getOrDefault("code", "0").toString().equals("2")) {
                return true;
            } else {
                throw new Exception("验证码发送异常：" + result.getOrDefault("msg", "").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    /*public void sendSms(String mobile,String code) {
        try {
            //配置阿里云
            Config config = new Config()
                    // 您的AccessKey ID
                    .setAccessKeyId(properties.getAccessKey())
                    // 您的AccessKey Secret
                    .setAccessKeySecret(properties.getSecret());
            // 访问的域名
            config.endpoint = "dysmsapi.aliyuncs.com";

            com.aliyun.dysmsapi20170525.Client client =  new com.aliyun.dysmsapi20170525.Client(config);

            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(mobile)
                    .setSignName(properties.getSignName())
                    .setTemplateCode(properties.getTemplateCode())
                    .setTemplateParam("{\"code\":\""+code+"\"}");
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response = client.sendSms(sendSmsRequest);

            SendSmsResponseBody body = response.getBody();

            System.out.println(body.getMessage());

        }catch (Exception e) {
            e.printStackTrace();
        }

    */


}
